package com.world;

import static org.lwjgl.opengl.GL11.GL_QUADS;
import static org.lwjgl.opengl.GL11.glBegin;
import static org.lwjgl.opengl.GL11.glColor3f;
import static org.lwjgl.opengl.GL11.glEnd;
import static org.lwjgl.opengl.GL11.glVertex3f;
import static org.lwjgl.opengl.GL11.glTexCoord2f;

import java.util.ArrayList;

import org.lwjgl.opengl.GL11;

import com.engine.I3DRenderer;
import com.engine.TextureAtlas;

public class ChunkRenderer implements I3DRenderer {
	private Chunk[][][] chunkData;
	private TextureAtlas texture;

	public ChunkRenderer(Chunk[][][] chunkData, TextureAtlas texture) {
		this.chunkData = chunkData;
		this.texture = texture;
	}

	public ChunkRenderer(int size, TextureAtlas texture) {
		this.texture = texture;
		chunkData = new Chunk[size][size][size];
		for (int x = 0; x < size; x++) {
			for (int y = 0; y < size; y++) {
				for (int z = 0; z < size; z++) {
					chunkData[x][y][z] = new Chunk(this);
				}
			}
		}
		for (int x = 0; x < size; x++) {
			for (int y = 0; y < size; y++) {
				for (int z = 0; z < size; z++) {
					if ((x + 1) != size)
						chunkData[x][y][z]
								.setRightChunk(chunkData[x + 1][y][z]);
					if ((x - 1) != -1)
						chunkData[x][y][z].setLeftChunk(chunkData[x - 1][y][z]);
					if ((z + 1) != size)
						chunkData[x][y][z]
								.setFrontChunk(chunkData[x][y][z + 1]);
					if ((z - 1) != -1)
						chunkData[x][y][z].setBackChunk(chunkData[x][y][z - 1]);
					if ((y + 1) != size)
						chunkData[x][y][z].setTopChunk(chunkData[x][y + 1][z]);
					if ((y - 1) != -1)
						chunkData[x][y][z]
								.setBottomChunk(chunkData[x][y - 1][z]);
				}
			}
		}
		for (int x = 0; x < size; x++) {
			for (int y = 0; y < size; y++) {
				for (int z = 0; z < size; z++) {
					chunkData[x][y][z].buildChunkMesh();
				}
			}
		}
	}

	public TextureAtlas getTextureAtlas() {
		return texture;
	}

	@Override
	public void render(int deltaTime) {
		texture.bindAtlas();
		for (int x = 0; x < chunkData.length; x++) {
			for (int y = 0; y < chunkData.length; y++) {
				for (int z = 0; z < chunkData.length; z++) {
					GL11.glPushMatrix();
					GL11.glTranslatef(x * Chunk.CHUNK_GL_SIZE
							* Chunk.CHUNK_SIZE, y * Chunk.CHUNK_GL_SIZE
							* Chunk.CHUNK_SIZE, z * Chunk.CHUNK_GL_SIZE
							* Chunk.CHUNK_SIZE);
					glColor3f(1.0f, 1.0f, 1.0f);
					glBegin(GL_QUADS);
					{
						// glTexParameteriv(GL11.GL_TEXTURE_2D,
						// GL11.GL_TEXTURE_CROP_RECT_OES, mCrop1, 0);
						ArrayList<Float> verts = chunkData[x][y][z]
								.getVertCoords();
						ArrayList<Float> texture = chunkData[x][y][z]
								.getTexCoords();
						for (int i = 0, t = 0; i < verts.size(); i += 3, t += 2) {
							// System.out.println(texture.get(t)+""+texture.get(t+1));
							glTexCoord2f(texture.get(t), texture.get(t + 1));
							glVertex3f(verts.get(i), verts.get(i + 1),
									verts.get(i + 2));
						}
					}
					glEnd();
					GL11.glPopMatrix();
				}
			}
		}

	}

}
